/**
 * @file main.c
 * @author ZMC (czm@comtika.com)
 * @brief encoding = GBK
 * @version 1.0.0.0
 * @date 2024-07-04
 * 
 * 
 */
#include "dev_4g.h"
#include "dev_lamp.h"
#include "iot_node.h"
#include "mconfig.h"
#include "platform.h"

#include <time.h>
#include <unistd.h>   //sleep

//#include "bsp_uart.h"
//#include "bsp_4g.h"

#include "platform.h"
//#include "simple_event.h"

#include "logfile.h"
#include "simple_event.h"
#include "time_taskv30.h"
#include "user_nv.h"
#include "test_mode.h"
#include "dev_attr.h"
#include "proto_upload_file.h"

#include "proto_plat.h"
#include "app_task.h"

#include "iotv30_app.h"



#define  SOFT_VERSION (0x0192) //2Bs


/////110.41.4.96:10096  ----服务器ip---------------


//variables
// osThreadId_t app_main_thread_id;
// static osTimerId_t  app_main_timer_id;

uint16_t app_get_ver(void)
{
    return SOFT_VERSION;
}


/**************************************************************************************************** */
#define DEFAULT_OTA_NAME "ota_package_fs.bin"
/**
 * @brief ֱcm_ota_firmware_erase() cant delete file immediately, delete manually.
 */
static void cm_ota_patch(void)
{
    // if(cm_fs_exist(DEFAULT_OTA_NAME))
    // {
    //     int32_t ret = cm_fs_delete(DEFAULT_OTA_NAME);
    //     if(ret >= 0)
    //     {
    //         cm_log_printf(0, "[del previous ota file ok]\n");
    //     }
    // }
}
    

/**
 * @brief 
 * 
 */
// static void power_on_info(void)
// {
//     char buf[CM_VER_LEN] = {0};
//     cm_fs_system_info_t info = {0, 0};
//     cm_heap_stats_t stats = {0};

//     cm_log_printf(0,"\n\n\n\n\n\n\n***************************************\n");
//     cm_sys_get_cm_ver(buf, CM_VER_LEN);
//     cm_log_printf(0,"CM OpenCPU, SDK VERSION:%s\n",buf);
//     cm_log_printf(0,"APP VERSION [%04X], compile time %s %s\n", , __DATE__, __TIME__);
//     cm_fs_getinfo(&info);
//     cm_mem_get_heap_stats(&stats);
//     cm_log_printf(0,"fs total:%d,remain:%d\n", info.total_size, info.free_size);
//     cm_log_printf(0,"heap total:%d,remain:%d\n",stats.total_size,stats.free);
//     cm_log_printf(0,"power on reason:%d\n",cm_pm_get_power_on_reason());
    
//     logfile_write(0,"\n\n\n[V%04X]power on reason:%d",, cm_pm_get_power_on_reason());
// }


#if  0
//int main(void *param)

int main(int argc, char **argv)
{
  //  cm_pm_work_lock();//disable sleep

 //   user_nv_init();//保存在flash中的参数，经纬度，电能等

//    power_on_info();

 //   uart_init(0,9600,2,elec_task);
    // uart_init(0,115200,0,uart_dbg_task);

#if (PRODUCT == XP3309_POWER)
  //  uart_init(1,9600,0,uart_xp3309_task);
  //  uart_init(1,115200,0,uart_dbg_task);
#endif

    cm_ota_patch();

//    timer_config();//10ms
    lamp_param_init();
   
    while (1)
    {     
    //  cat1_state_machine();

        plat_conn_manager();
   //   simple_event_handle();

        logfile_upload_process();

       // osDelay(2);//10ms--tick=5ms
		//sleep(1);//sleep 1sec 系统响应很慢
		usleep(10000);    //usleep 10000usec= 10msec 系统响应可接受--------

    }

	return 0;
}

#endif


void  iotv30_app_init(void)
{
//    cm_pm_work_lock();//disable sleep
//    power_on_info();  //打印上电信息
//    user_nv_init();//保存在flash中的参数，经纬度，电能等


    user_config_init();
    time_task_init();

    cm_ota_patch();
//    timer_config();//10ms----配置定时器
    lamp_param_init();  

    dev_4g_init( );

    simple_event_init( );//=======事件触发逻辑初始化============

    iot_Node_Init( );

  // iot_Node_Test( );

}




void  iotv30_app_process(void)
{
//  cat1_state_machine( );////=======4g模块联网逻辑==目前不用==========

  //  plat_conn_manager( );//////=======登录心跳逻辑============

   // simple_event_handle( );////=======事件触发逻辑============

  //  logfile_upload_process( );////======上传固件逻辑======----这里导致段错误===-----查找到了有内存泄漏===

    //plat_report_attr_process( );

     iot_Node_UpdateParamProcess( );

    // osDelay(2);//10ms--tick=5ms
    //sleep(1);//sleep 1sec 
    //usleep(10000);    //usleep 10000usec= 10msec 

}











